package cinema.service;

import cinema.model.*;
import cinema.util.*;
import java.util.*;

public class TicketService {
    private static final Map<String, Ticket> tickets = new HashMap<>();

    public static void sellTicketUI() {
        ShowtimeService.listShowtimes();
        int idx = InputUtil.readInt("选择场次序号: ");
        if (idx<0 || idx>=ShowtimeService.getAll().size()) return;
        Showtime s = ShowtimeService.getAll().get(idx);
        s.printSeatMap();
        int row = InputUtil.readInt("行: ");
        int col = InputUtil.readInt("列: ");
        if (s.isSold(row, col)) { System.out.println("座位已售"); return; }
        s.sellSeat(row, col);
        String id = UUID.randomUUID().toString();
        tickets.put(id, new Ticket(id, s, new Seat(row, (char)('A'+col-1))));
        System.out.println("出票成功 票号: "+id+" 金额: "+s.getPrice());
        Logger.log("售票", id);
    }

    public static void refundTicketUI() {
        String id = InputUtil.readLine("票号: ");
        Ticket t = tickets.remove(id);
        if (t==null) { System.out.println("票号不存在"); return; }
        t.getShowtime().refundSeat(t.getSeat().getRow(), t.getSeat().getCol());
        System.out.println("退票成功");
        Logger.log("退票", id);
    }

    public static boolean hasSold(Showtime s) {
        return tickets.values().stream().anyMatch(t -> t.getShowtime()==s);
    }

    public static void showSaleData() {
        System.out.println("总售票: "+tickets.size());
    }
}
